<?php

namespace app\service\article;

use app\service\admin\AdminLogic;
use app\service\base\BaseLogic;
use app\service\category\CategoryLogic;
use think\Exception;

class ArticleLogic extends BaseLogic
{
    const IS_OPEN_COMMENT_NO = 0;
    const IS_OPEN_COMMENT_YES = 1;

    const IS_TOP_NO = 0;
    const IS_TOP_YES = 1;

    const IS_RECOMMENDED_NO = 0;
    const IS_RECOMMENDED_YES = 1;

    public function getList($title, $arrCateIds = [], $limit)
    {
        $arrWhere = [];
        if ($title) {
            $arrWhere[] = ['title', 'like', "%{$title}%"];
        }
        if ($arrCateIds) {
            $arrWhere[] = ['category_id', 'in', $arrCateIds];
        }
        $field = 'A.id, title, `desc`, is_open_comment, is_top, is_recommended, view_count, like_count, collect_count, recommend_count, admin_id, create_dt, group_concat(category_id) as arr_cate_id';
        $arrData = (new ArticleModel())->getAll($arrWhere, $field, $limit);
        if (empty($arrData['data'])) {
            return $arrData;
        }
        $arrAdminIds = array_column($arrData['data'], 'admin_id');
        $arrAdmin    = AdminLogic::getInstance()->getListByIds($arrAdminIds);
        $arrAdmin    = array_column($arrAdmin, 'real_name', 'id');
        $arrCategory = CategoryLogic::getInstance()->getAllCategory();
        $arrCategory = array_column($arrCategory, 'name', 'id');
        $arrData['data'] = array_map(function ($arrVal) use ($arrAdmin, $arrCategory) {
            $arrVal['is_open_comment_desc'] = yes_or_no($arrVal['is_open_comment']);
            $arrVal['is_top_desc']          = yes_or_no($arrVal['is_top']);
            $arrVal['is_recommended_desc']  = yes_or_no($arrVal['is_recommended']);
            $arrVal['admin_name']           = $arrAdmin[$arrVal['admin_id']] ?? '';
            if (!empty($arrVal['arr_cate_id'])) {
                $arrCateId                      = explode(',', $arrVal['arr_cate_id']);
                $arrVal['cate_desc']            = array_map(function ($cateId) use ($arrCategory) {
                    return '<a class="cate">' . ($arrCategory[$cateId] ?? '') . '</a>';
                }, $arrCateId);
                $arrVal['cate_desc'] = implode('&nbsp &nbsp', $arrVal['cate_desc']);
            }
            return $arrVal;
        }, $arrData['data']);
        return $arrData;
    }

    public function add($title, $desc, $content, $arrCategoryId, $img, $isOpenComment, $isTop, $isRecommended, $adminId)
    {
        if (!$this->check($title)) {
            throw new Exception('已存在同名文章');
        }
        $dateTime = date('Y-m-d H:i:s');
        $arrData = [
            'title'           => $title,
            'desc'            => $desc,
            'content'         => $content,
            'cover_image'     => $img,
            'is_open_comment' => $isOpenComment,
            'is_top'          => $isTop,
            'is_recommended'  => $isRecommended,
            'admin_id'        => $adminId,
            'publish_dt'      => $dateTime,
            'create_dt'       => $dateTime,
            'update_dt'       => $dateTime,
        ];
        $id = (new ArticleModel())->add($arrData);
        if (!empty($arrCategoryId)) {
            foreach ($arrCategoryId as $catId) {
                ArticleCategoryLogic::getInstance()->add($id, $catId);
            }
        }

        return true;
    }

    public function edit($id, $title, $desc, $content, $arrCategoryId, $isOpenComment, $isTop, $isRecommended, $adminId, $img)
    {
        if (!$this->check($title, $id)) {
            throw new Exception('已存在同名文章');
        }
        $dateTime = date('Y-m-d H:i:s');
        $arrData = [
            'title'           => $title,
            'desc'            => $desc,
            'content'         => $content,
            'is_open_comment' => $isOpenComment,
            'is_top'          => $isTop,
            'is_recommended'  => $isRecommended,
            'admin_id'        => $adminId,
            'update_dt'       => $dateTime,
        ];
        if (!empty($img)) {
            $arrData['cover_image'] = $img;
        }
        (new ArticleModel())->edit($id, $arrData);
        ArticleCategoryLogic::getInstance()->delByIds($id);
        if (!empty($arrCategoryId)) {
            foreach ($arrCategoryId as $catId) {
                ArticleCategoryLogic::getInstance()->add($id, $catId);
            }
        }

        return true;
    }

    public function del($id)
    {
        if (empty($id)) {
            throw new \Exception('更新参数错误');
        }
        (new ArticleModel())->delById($id);
        ArticleCategoryLogic::getInstance()->delByIds($id);
        return true;
    }

    public function check($title, $id = 0)
    {
        $arrWhere = [];
        if ($id) {
            $arrWhere[] = ['id', '<>', $id];
        }
        $arrWhere[] = ['title', '=', $title];
        $arrData = (new ArticleModel())->getRow($arrWhere);
        return !empty($arrData) ? false : true;
    }

    public function getRowById($id)
    {
        $arrWhere = [];
        $arrWhere['id'] = $id;
        return (new ArticleModel())->getRow($arrWhere);
    }
}